﻿/*****************************************************************************
Copyright (c) 2011 SmartVault, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartVault.Net
{
    public class MimeTypes
    {
        private string _type;
        private string _subType;
        private string _mime;
        private string _other;

        private MimeTypes(string mime)
        {
            _mime = mime;
            if (mime.Contains(";"))// throw new ArgumentException();
            {
                _other = mime.Substring(mime.IndexOf(';') + 1);
                mime = mime.Substring(0, mime.IndexOf(';'));
            }
            _mime = mime.Trim();

            if (!string.IsNullOrEmpty(mime))
            {
                string[] tmp = _mime.Split('/');
				if(tmp.Length == 1)
				{
					_type = ApplicationOctstream._mime;
					_subType = tmp[0];
				}
				else
				{
					_type = tmp[0];
					_subType = tmp[1];
				}
            }
            else
            {
                _mime = ApplicationOctstream._mime;
                _type = ApplicationOctstream._type;
                _subType = ApplicationOctstream._subType;
            }
        }

        public static bool operator ==(MimeTypes lhs, MimeTypes rhs)
        {
            if (((object)lhs) == null) return ((object)rhs) == null;
            return lhs.Equals(rhs);
        }

        public static bool operator !=(MimeTypes lhs, MimeTypes rhs)
        {
            return !(lhs == rhs);
        }

        public static explicit operator MimeTypes(string type)
        {
            return type == null ? AnyType : new MimeTypes(type);
        }

        public string Type { get { return _type; } }
        public string SubType { get { return _subType;  } }

        public override string ToString()
        {
            return _mime;
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            return obj.ToString().Equals(this.ToString());
        }

        public bool IsXml
        {
            get
            {
                // RFC 3023
                return this.Equals(ApplicationXml) || this.Equals(TextXml);
            }
        }

        public bool IsJson
        {
            get
            {
                return this.Equals(ApplicationJson);
            }
        }

        public bool IsProtobuf
        {
            get
            {
                return this.Equals(ApplicationProtobuf);
            }
        }

        public bool IsHtml
        {
            get
            {
                return this.Equals(TextHtml);
            }
        }

        public bool IsXHtml
        {
            get
            {
                return this.Equals(ApplicationXhtmlAndXml);
            }
        }

        public bool IsAnyType
        {
            get
            {
                return this.Equals(AnyType);
            }
        }

        public bool IsMultipart
        {
         get { return this.Type.ToLower() == "multipart"; }   
        }

        public bool IsUrlEncodedForm { get { return this == UrlEncodedForm; } }
        public bool IsMultipartMixed { get { return this == MultipartMixed; } }
        public bool IsMultipartFormData { get { return this == MultipartFormData;  } }

        public bool IsNotDefined
        {
            get { return string.IsNullOrEmpty(this.ToString()); }
        }

        public static MimeTypes ApplicationXml = new MimeTypes("application/xml");
        public static MimeTypes TextXml = new MimeTypes("text/xml");
        public static MimeTypes ApplicationBinary = new MimeTypes("application/binary");
        public static MimeTypes ApplicationJson = new MimeTypes("application/json");
        public static MimeTypes ApplicationProtobuf = new MimeTypes("application/x-protobuf");
        public static MimeTypes TextHtml = new MimeTypes("text/html");
        public static MimeTypes ApplicationXhtmlAndXml = new MimeTypes("application/xhtml+xml");
        public static MimeTypes MultipartMixed = new MimeTypes("multipart/mixed");
        public static MimeTypes TextPlain = new MimeTypes("text/plain");
        public static MimeTypes AnyType = new MimeTypes("*/*");
        public static MimeTypes ApplicationOctstream = new MimeTypes("application/octet-stream");
        public static MimeTypes UrlEncodedForm = new MimeTypes("application/x-www-form-urlencoded");
        public static MimeTypes MultipartFormData = new MimeTypes("multipart/form-data");
    }
}
